<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateDistributionStatisticsRequest;
use App\Http\Requests\UpdateDistributionStatisticsRequest;
use App\Models\DistributionStatistics;
use App\Models\Order;
use App\Repositories\DistributionStatisticsRepository;
use App\Http\Controllers\AppBaseController;
use App\User;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class DistributionStatisticsController extends AppBaseController
{
    /** @var  DistributionStatisticsRepository */
    private $distributionStatisticsRepository;

    public function __construct(DistributionStatisticsRepository $distributionStatisticsRepo)
    {
        $this->distributionStatisticsRepository = $distributionStatisticsRepo;
    }

    /**
     * Display a listing of the DistributionStatistics.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $model = new DistributionStatistics();

        $nickname = request('nickname',false);
        if($nickname){
            $user = User::where('nickname', $nickname)->first();
            if($user){
                $model = $model->where('user_id',$user->id);
            }else{
                $model = $model->where('id',0);
            }
        }

        $name = request('name',false);
        if($name){
            $user = User::where('name', $name)->first();
            if($user){
                $model = $model->where('user_id',$user->id);
            }else{
                $model = $model->where('id',0);
            }
        }

        $phone = request('phone',false);
        if($phone){
            $user = User::where('phone', $phone)->first();
            if($user){
                $model = $model->where('user_id',$user->id);
            }else{
                $model = $model->where('id',0);
            }
        }
        $distributionStatistics = $model->orderBy('date','desc')
            ->orderBy('shipping_number','desc')
            ->orderBy('pairing_number','desc')
            ->orderBy('verify_number','desc')
            ->paginate(20);

        $distributionStatistics->load('user');
        return view('distribution_statistics.index')
            ->with('distributionStatistics', $distributionStatistics)->with('request',$request->all());
    }

    /**
     * Show the form for creating a new DistributionStatistics.
     *
     * @return Response
     */
    public function create()
    {
        return view('distribution_statistics.create');
    }

    /**
     * Store a newly created DistributionStatistics in storage.
     *
     * @param CreateDistributionStatisticsRequest $request
     *
     * @return Response
     */
    public function store(CreateDistributionStatisticsRequest $request)
    {
        $input = $request->all();

        $distributionStatistics = $this->distributionStatisticsRepository->create($input);

        Flash::success('Distribution Statistics saved successfully.');

        return redirect(route('distributionStatistics.index'));
    }

    /**
     * Display the specified DistributionStatistics.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $distributionStatistics = $this->distributionStatisticsRepository->findWithoutFail($id);

        if (empty($distributionStatistics)) {
            Flash::error('Distribution Statistics not found');

            return redirect(route('distributionStatistics.index'));
        }
        $distributionStatistics->info = json_decode($distributionStatistics->json,true);
        $_orders = Order::whereIn('id',$distributionStatistics->info['order_ids'])->get();
        $orders = [];
        foreach ($_orders as $order) {
            $orders[$order->id] = $order;
        }
        return view('distribution_statistics.show')
            ->with('distributionStatistics', $distributionStatistics)
            ->with('orders',$orders);
    }

    /**
     * Show the form for editing the specified DistributionStatistics.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $distributionStatistics = $this->distributionStatisticsRepository->findWithoutFail($id);

        if (empty($distributionStatistics)) {
            Flash::error('Distribution Statistics not found');

            return redirect(route('distributionStatistics.index'));
        }

        return view('distribution_statistics.edit')->with('distributionStatistics', $distributionStatistics);
    }

    /**
     * Update the specified DistributionStatistics in storage.
     *
     * @param  int              $id
     * @param UpdateDistributionStatisticsRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateDistributionStatisticsRequest $request)
    {
        $distributionStatistics = $this->distributionStatisticsRepository->findWithoutFail($id);

        if (empty($distributionStatistics)) {
            Flash::error('Distribution Statistics not found');

            return redirect(route('distributionStatistics.index'));
        }

        $distributionStatistics = $this->distributionStatisticsRepository->update($request->all(), $id);

        Flash::success('Distribution Statistics updated successfully.');

        return redirect(route('distributionStatistics.index'));
    }

    /**
     * Remove the specified DistributionStatistics from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $distributionStatistics = $this->distributionStatisticsRepository->findWithoutFail($id);

        if (empty($distributionStatistics)) {
            Flash::error('Distribution Statistics not found');

            return redirect(route('distributionStatistics.index'));
        }

        $this->distributionStatisticsRepository->delete($id);

        Flash::success('Distribution Statistics deleted successfully.');

        return redirect(route('distributionStatistics.index'));
    }
}
